Skip to content

fix(studio): caption editing works in a sealed preview - #8002

Merged
miguel-heygen merged 7 commits into
mainfrom
hfsupabase/captions-editing-port
Nov 17, 2026
Merged

miguel-heygen merged 7 commits into
mainfrom
hfsupabase/captions-editing-port

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

What

Caption editing works when Studio's preview runs sealed (an opaque-origin frame). Before this, a sealed preview never entered caption edit mode, so the caption overlay and its property panel never showed.

With a same-origin preview nothing changes: every caller keeps its direct path, and the editing port is used only when previewFrameOpaque(iframe) is true (the same split as useElementPicker).

How

Two new editing-port request kinds, each handled in its own file under packages/core/src/runtime/editing/:

  • caption-words: Studio sends how many words its model has per .caption-group. The frame returns the boxes of words in visible groups. A group written as plain text is split into word spans inside the frame, as Studio's same-origin path does today.
  • caption-transform: reads, or with set writes, a word's wrapper transform (x, y, scale, rotation). It uses the page's gsap.set when the page has GSAP, else a CSS transform, as CaptionOverlayUtils does. It returns the transform after the write and the word's box.

Params are validated like the existing kinds: indices are bounded integers, transform values are finite and within MAX_DRAFT_PX, at most MAX_DESCRIBE_HANDLES entries per request, and unknown keys are refused.

Studio side:

  • Overlay: word boxes are read with one request in flight; triggers that arrive meanwhile collapse into one more read. A drag reads the word on press, and moves made before that reply lands are applied once it does. Transform writes go one request at a time per frame, and a waiting write to the same word is replaced by the newer one, so a drag never piles up requests. Pointer-up stores the transform from the last write's reply. Keyboard nudges run in order, so quick repeats all count.
  • Property panel: X, Y, scale and rotation edits go through caption-transform.
  • Undo (applyCaptionModelToIframe) writes each word's restored transform through the same queue.
  • Detection: the existing query, describe, ancestors and computed-styles kinds read the caption groups, words, composition hosts and root size. The groups and words are rebuilt, in document order and nesting, in a detached document, and their first word's and group's computed styles are served by a stand-in getComputedStyle. parseCaptionComposition then runs unchanged. Detection keeps copies of the parser's two selectors, since the parser does not export them.

Kept direct reads (they work on an opaque frame): the two e.source !== iframe.contentWindow checks in CaptionOverlay.tsx and useCaptionDetection.ts compare window identity, which is allowed across origins. The direct same-origin branches stay as they were.

Behavior differences, sealed only:

  • With no GSAP on the page, a sealed property-panel edit moves the word with a CSS transform. The same-origin panel path writes only through GSAP.
  • The panel finds a word the way the overlay does (getWordEl's counting).

Package size: the inspector inlines the runtime script, so the two new kinds grow its unpacked size; it stays within the current budget in scripts/package-size-budgets.json (the size check passes), so this PR does not change the budget.

Bound: sealed detection reads at most 2048 caption groups and words together (one query page). A larger caption composition does not enter edit mode while sealed.

Before

A sealed preview of a caption composition: Studio never enters caption edit mode.

Before: the sealed caption preview stays out of caption edit mode

After

The same preview enters caption edit mode, and a clicked word shows its selection box and handles. (A "Caption changes couldn't be saved" notice from this local setup was closed before the shot; it shows the same way with a same-origin preview.)

After: the sealed preview in caption edit mode with one word selected

Tests

fails without the fix: packages/studio/src/captions/components/CaptionOverlay.sealed.test.ts ("caption overlay shows + edits sealed") fails on main with AssertionError: expected [] to have a length of 2 but got +0, because the sealed preview never enters caption edit mode.

  • CaptionOverlay.sealed.test.ts ("caption overlay shows + edits sealed", the first commit): runs the real runtime editing agent behind a frame Studio cannot read. It covers detection, boxes, a drag that moves before the press read returns, a panel X edit, an arrow-key nudge and undo. On main it fails because the sealed preview never enters edit mode (no boxes).
  • editing/captionWords.test.ts: the in-frame split, visibility, CSS and GSAP writes, and missing words.
  • editingAgent.test.ts: new invalid-params cases for both kinds; hello lists them.

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 100 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (8)

@terencecho terencecho left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed pinned head c37aea2. No correctness, security, or data-loss blocker found. The new caption requests use the existing editing-port boundary with validated parameters; the same-origin paths remain direct. Sealed detection, overlay measurements, queued transforms, drag completion, property-panel transforms, sequential nudges, and undo were reviewed.

Validation: full workspace build passed; 162 targeted core runtime/protocol tests and 111 caption/Studio tests passed (273 total). Restoring the base detection hook makes CaptionOverlay.sealed.test.ts fail with zero boxes instead of two, confirming the required regression. Restored the reviewed source afterward and verified a clean tracked diff.

Current CI has 85 successes, 11 skips, 1 cancellation and 2 failures. The failed Windows engine-cli-2 log identifies urlDownloader.runtimes.test.ts, the Bun private-address test timing out at 5000 ms; the other failure is the Windows aggregate. That file is outside this patch. I did not rerun Windows or exercise a real browser/save-to-disk flow; the sealed integration is a happy-dom test with the real runtime agent. The documented 2048 combined group/word detection bound remains a limitation.

— tai (automated review)

@miguel-heygen
miguel-heygen force-pushed the hfsupabase/captions-editing-port branch 2 times, most recently from 2e92cba to 0fd6d09 Compare November 17, 2026 04:42

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved at 0fd6d09.

  • Params: captionWordsParams and captionTransformParams are registered in PARAM_PARSERS, so they're checked before any handler runs. Indices are safe integers under 100,000, entries are capped at MAX_DESCRIBE_HANDLES, set keys are whitelisted and extra keys are refused.
  • Port: no new listener or handshake change. The frame returns only rects and transforms, which describe already exposes.
  • Writes: no text or timings come back from the frame, and persisted writes go only to caption-overrides.json.
  • Studio side: frame data is shape-checked (readFrameWord, readFrameWordBoxes, isPlainRecord).
  • Rebase: the diff is 12 files, all caption or editing-port scope, and the size-budget file isn't touched.
  • Test: the fails without the fix: line is present.

Should-fix:

  1. Stale detection: the sealed branch of useCaptionDetection.ts (loadCaptions, ~L160) makes several round trips, checks only isEditMode, and isn't cancelled by the effect cleanup. Switch composition mid-detection and A's chain can call setModel / setEditMode(true) after B's reset. Add an epoch like #7999's reads.current.
  2. Writes after unmount: in CaptionOverlay.tsx, the nudging chain and the pointer-up sealed.last.then(storeStyle) ignore disposed, so they can store a stale style after unmount.
  3. Unbounded reply values: readFrameWord only checks that values are finite. One huge frame-reported value gets persisted, then captionTransformParams rejects every later write and undo for that word. Apply the same 100,000 bound to replies.

…o moves on

A caption detection, nudge or drag still waiting on a sealed preview no longer
writes after its effect is gone, and a frame-reported word transform past the
frame's own write bound is refused instead of stored. Each has a test against
the real editing agent that holds the frame's answer open.
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Thanks. All three are fixed in 1a9df6c, each with a test in CaptionOverlay.sealed.test.ts that runs against the real editing agent behind an opaque frame. To cover the races, the test harness can now hold the frame's answer to a chosen request until the test lets it through.

  1. Stale detection. Each detection effect now has its own disposed flag, set in its cleanup. It plays the same role as an epoch, scoped to the effect run. The sealed chain checks it before starting loadCaptions, and loadCaptions checks it again before parsing and before any store write. The same-origin fetch had the same race and is covered by the same checks.
    Test: a detection still waiting on the frame when Studio unmounts it loads no captions holds the frame's answer to the detection's last query, unmounts, then lets the answer through. With the checks removed it fails: expected "vi.fn()" to not be called at all, but actually been called 1 times.
  2. Writes after unmount. The nudge chain checks the effect's disposed before it starts a nudge and before it stores one. Pointer-up stores the drag only while the edit-mode effect is mounted (liveRef).
    Tests: a sealed nudge the frame answers after the overlay unmounts stores no style and a sealed drag the frame answers after the overlay unmounts stores no style each hold the write's answer until after unmount. Without the nudge checks the nudge test fails with expected 1 to be undefined. Without the pointer-up check the drag test fails with expected 30 to be undefined.
  3. Unbounded replies. readFrameWord now holds reply transforms to the frame's own write bound. MAX_CAPTION_TRANSFORM is exported from captionTransform.ts through the editing port, so both sides use the same number. Rects are still checked only for being finite, because they are never stored.
    Test: a frame word past the write bound is not stored, and a word within it still is sets one word to x = 200000 in the frame. Pressing it stores nothing, and dragging the other word still stores its move. With the old finite-only check it fails: expected 200000 to be undefined.

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at 1a9df6c. This covers my three should-fixes.

  • Detection: useCaptionDetection takes a disposed flag that the cleanup sets, so a detection still in flight can't apply the old composition's model or enter edit mode.
  • Late replies: CaptionOverlay checks disposed in the nudge chain and liveRef on the pointer-up sealed.last, so a late reply stores nothing.
  • Bounds: readFrameWord applies the shared MAX_CAPTION_TRANSFORM (100,000) to x, y, scale and rotation, so an out-of-range reply can't be stored and block later writes.

Tests cover all three.

…me word helpers

Keeps the caption overlay under the studio file size limit and its comment share at the base. No behaviour change.
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Thanks for the re-approval. The new head, 6194768, only moves code to pass the size and comment checks. The sealed nudge queue moved from CaptionOverlay.tsx into CaptionOverlayUtils.ts as sealedNudger, next to nudgeFrameWord, and two comments the code already states were dropped. Behaviour is unchanged. The five sealed caption tests still pass, and the nudge test still fails with the queue's checks removed (expected 1 to be undefined).

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at 6194768. This is a refactor only: the sealed nudge queue moved to sealedNudger in CaptionOverlayUtils.ts. The same live() check runs before each nudge starts and before it stores, and nudges stay serialized.

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at d1f049e. The only change is a one-line refactor in CaptionOverlayUtils.ts, keeping the nudge request inside its helper file. Behaviour is unchanged.

@miguel-heygen
miguel-heygen added this pull request to the merge queue Nov 17, 2026
Merged via the queue into main with commit 9150212 Nov 17, 2026
97 checks passed
@miguel-heygen
miguel-heygen deleted the hfsupabase/captions-editing-port branch November 17, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants